<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
  </head>
  <body>
    <button>测试拦截器</button>
    <script>
      //配置基本路径和超时时间
      const request = axios.create({
        baseURL: 'http://localhost:8081',
        timeout: 30000
      })


      //添加请求拦截器
      request.interceptors.request.use((config) => {
          console.log('请求拦截器回调')
          config.headers['token'] = 'atguigu'
          console.log('请求:', Date.now(),config.url)
          return config;
        })
        
        request.interceptors.response.use(
        response=>{
          console.log('响应拦截器成功回调')
          return response.data
        },
        error =>{
          console.log('响应拦截器失败的回调')
          alert('请求错误：' + error.message)
          return Promise.reject(error)
        }
      )


      const btn = document.querySelector('button');


      btn.onclick = function(){
         request({
          url: 'delay-server/aaa',
          // url: '/delay-server2/aaa',
          method: 'post',
          params: {a: 1, b: 2},
          data: {c: 3, d: 4}
        }).then(
          response => {
            console.log('具体请求的成功回调', response)
          },
          error => {
            console.log('具体请求的失败回调', error)
          }
        )
      };













      
    </script>
  </body>
</html>
